package com.jucai.pppeiyouclient.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.view.activity.FindPayOrderActivity;
import com.jucai.pppeiyouclient.view.activity.RechargeActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, null);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Bundle bundle = new Bundle();
            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                FindPayOrderActivity.payWxDone();
                RechargeActivity.payWxDone(true);
                //ApplyPayActivity.payWxDone();
            } else if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {//用户取消
                Toast.makeText(WXPayEntryActivity.this, "微信支付取消！", Toast.LENGTH_LONG).show();
            } else if (resp.errCode == BaseResp.ErrCode.ERR_AUTH_DENIED) {//用户拒绝
                Toast.makeText(WXPayEntryActivity.this, "微信支付拒绝！", Toast.LENGTH_LONG).show();

            } else {
                if (TextUtils.isEmpty(resp.errStr))
                    Toast.makeText(WXPayEntryActivity.this, "微信支付失败！如需帮助请联系客服！", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(WXPayEntryActivity.this, resp.errStr, Toast.LENGTH_LONG).show();
            }
        }
        finish();
    }
}